//
//  AWHomeOriginalView.swift
//  WeiBo
//
//  Created by AaronWei on 2017/6/8.
//  Copyright © 2017年 AaronWei. All rights reserved.
//

import UIKit
import SnapKit

class AWHomeOriginalView: UIView {
    
    var statusesViewModel: AWStatusesViewModel? {
        
        didSet {
        
            guard let currentViewModel = statusesViewModel else { return }
            
            if let imageUrl = currentViewModel.status?.user?.avatar_large {
                
                userImageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: #imageLiteral(resourceName: "avatar_default_big"))
                
            }
            
            screenNameLabel.text = currentViewModel.status?.user?.screen_name
            
            contentLabel.text = currentViewModel.status?.text
            
            mbrankImageView.image = currentViewModel.mbrankImage
            
            verifiedTypeImageView.image = currentViewModel.verifiedTypeImage
            
            sourceLabel.text = currentViewModel.sourceStr
            
            timeLabel.text = currentViewModel.timeText
            
            timeLabel.textColor = currentViewModel.timeTextColor
            
            retweeteBottomConstraints?.deactivate()
            
            if let picUrls = statusesViewModel?.status?.pic_urls, picUrls.count > 0 {
                
                pictureView.isHidden = false
                
                self.snp.makeConstraints({ (make) in
                    
                    retweeteBottomConstraints = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                    
                })
                
                pictureView.picUrls = picUrls
                
            } else {
                
                pictureView.isHidden = true
                
                self.snp.makeConstraints({ (make) in
                    
                    retweeteBottomConstraints = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                    
                })
                
            }
            
        }
        
    }
    
    var retweeteBottomConstraints: Constraint?
    
    fileprivate lazy var userImageView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
    
    fileprivate lazy var verifiedTypeImageView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "avatar_vip"))
    
    fileprivate lazy var screenNameLabel: UILabel = UILabel(title: "哈哈", fontSize: 14, textColor: UIColor.darkGray, maxWidth: nil)
    
    fileprivate lazy var timeLabel: UILabel = UILabel(title: "刚刚", fontSize: 12, textColor: UIColor.orange, maxWidth: nil)
    
    fileprivate lazy var sourceLabel: UILabel = UILabel(title: "新浪微博", fontSize: 12, textColor: UIColor.lightGray, maxWidth: nil)
    
    fileprivate lazy var mbrankImageView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "common_icon_membership"))
    
    fileprivate lazy var contentLabel: UILabel = UILabel(title: "测试一下下啦", fontSize: 14, textColor: UIColor.darkGray, maxWidth: nil)
    
    fileprivate lazy var pictureView: AWHomePictureView = {
        
        let collctionView = AWHomePictureView()
        
        collctionView.backgroundColor = self.backgroundColor
        
        return collctionView
        
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupUI()
        
    }
    
    private func setupUI() {
        
        backgroundColor = UIColor.white
        
        addSubview(userImageView)
        
        addSubview(verifiedTypeImageView)
        
        addSubview(screenNameLabel)
        
        addSubview(timeLabel)
        
        addSubview(sourceLabel)
        
        addSubview(mbrankImageView)
        
        addSubview(contentLabel)
        
        addSubview(pictureView)
        
        contentLabel.textAlignment = .natural
        
        userImageView.layer.cornerRadius = 25
        
        userImageView.layer.masksToBounds = true
        
        userImageView.snp.makeConstraints { (make) in
            
            make.top.leading.equalTo(self).offset(HomeCellMargin)
            make.size.equalTo(CGSize(width: 50, height: 50))
            
        }
        
        verifiedTypeImageView.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(userImageView.snp.trailing).offset(-8)
            make.centerY.equalTo(userImageView.snp.bottom).offset(-8)
            
        }
        
        screenNameLabel.snp.makeConstraints { (make) in
            
            make.top.equalTo(userImageView).offset(8)
            make.leading.equalTo(userImageView.snp.trailing).offset(HomeCellMargin)
            
        }
        
        timeLabel.snp.makeConstraints { (make) in
            
            make.bottom.equalTo(userImageView).offset(-8)
            make.leading.equalTo(screenNameLabel)
            
        }
        
        sourceLabel.snp.makeConstraints { (make) in
            
            make.bottom.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp.trailing).offset(HomeCellMargin)
            
        }
        
        mbrankImageView.snp.makeConstraints { (make) in
            
            make.top.equalTo(screenNameLabel)
            make.leading.equalTo(screenNameLabel.snp.trailing).offset(HomeCellMargin)
            
        }
        
        contentLabel.snp.makeConstraints { (make) in
            
            make.top.equalTo(userImageView.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(userImageView)
            make.trailing.equalTo(self).offset(-10)
            
        }
        
        pictureView.snp.makeConstraints { (make) in
            
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(HomeCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
            
        }
        
        self.snp.makeConstraints { (make) in
            
            retweeteBottomConstraints = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
            
        }
        
    }

}
